package com.example.boot1.conttoller;
import com.example.boot1.bean.*;
import com.example.boot1.service.VueDishesService;
import com.example.boot1.service.VueUnitService;
import com.example.boot1.service.impl.VueUnitServiceImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* (vue_unit)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/vue")
public class VueUnitController {
/**
* 服务对象
*/
    @Autowired
    private VueUnitService vueUnitService;

    @Autowired
    private VueDishesService vueDishesService;

    /**
    * 通过主键查询单条数据
    *
    * @param id 主键
    * @return 单条数据
    */
    @GetMapping("/selectOne")
    public VueUnit selectOne(Integer id) {
      return vueUnitService.selectByPrimaryKey(id);
    }

    /**
     * 查询全部菜品类目
     * token 验证先不用实现
     * @return
     */
    @GetMapping("/getCate")
    @ResponseBody
    public ResponseEntity<BackDataBean> getCate(HttpServletRequest request){

        List<VueDishes> vtList = vueDishesService.getCate();

        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        if (null!=vtList){
            backDataBean = new BackDataBean();
            backDataBean.setCode(200);
            backDataBean.setMsg("查询成功！");
            backDataBean.setResult(vtList);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
        }else {
            backDataBean.setCode(202);
            backDataBean.setMsg("服务器错误！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
        }

        return responseEntity;
    }
    /**
     * 查询全部单位
     * token 验证先不用实现
     * @return
     */
    @GetMapping("/getUnit")
    @ResponseBody
    public ResponseEntity<BackDataBean> getUnit(HttpServletRequest request){

        List<VueUnit> utList = vueUnitService.getUnit();

        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        if (null!=utList){
            backDataBean = new BackDataBean();
            backDataBean.setCode(200);
            backDataBean.setMsg("查询成功！");
            backDataBean.setResult(utList);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
        }else {
            backDataBean.setCode(202);
            backDataBean.setMsg("服务器错误！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
        }

        return responseEntity;
    }

    /**
     * 自定义菜品保存
     * 同样将token置后
     * @param request
     * @param vueUnitRequestBean
     * @return
     */
    @PostMapping("/addUnit")
    @ResponseBody
    public ResponseEntity<BackDataBean> addUnit(HttpServletRequest request,
                                                @RequestBody VueUnitRequestBean vueUnitRequestBean){

        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        if (null==vueUnitRequestBean.getUnit()  || "".equals(vueUnitRequestBean.getUnit())){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请检查输入！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            return  responseEntity;
        }
        try {

            VueUnit vueUnit = new VueUnit();
            vueUnit.setUid("uid00001");
            vueUnit.setValue(vueUnitRequestBean.getUnit());
            vueUnit.setLaber(vueUnitRequestBean.getUnit());
            List<VueUnit> vlist = vueUnitService.findByValue(vueUnitRequestBean.getUnit());

            if ( null != vlist && vlist.size()>0){
                backDataBean = new BackDataBean();
                backDataBean.setCode(202);
                backDataBean.setMsg("添加的 "+vueUnitRequestBean.getUnit()+" 已存在！");
                backDataBean.setResult(null);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }else{
                int utList = vueUnitService.insert(vueUnit);

                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("保存成功！");
                backDataBean.setResult(utList);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }
        }catch (Exception e) {

            backDataBean.setCode(202);
            backDataBean.setMsg("服务器错误！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            e.printStackTrace();
        }
        return responseEntity;
    }
}
